package com.ldj.struts.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.common.Upload;
import com.ldj.hibernate.DataDictionary;
import com.ldj.hibernate.DataDictionaryDAO;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.FunctionMenuDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.hibernate.UnitUser;
import com.ldj.hibernate.UnitUserApply;
import com.ldj.hibernate.UnitUserDAO;
import com.ldj.hibernate.UserAreaScope;
import com.ldj.struts.form.EmployerInfoForm;

public class EmployerInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		String code = request.getParameter("code");
		List list = dao.findAllPage(code, eForm, null);
		request.setAttribute("intPage", String.valueOf(dao.intPage));
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	//查找没有分配用户的单位
	public ActionForward listNotUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		UnitUserDAO uDAO = new UnitUserDAO(request, response);
		DataDictionaryDAO ddDAO = new DataDictionaryDAO(request, response);
	//	List<UnitUser> uList= uDAO.findAllUnit();
		String areaCode = request.getParameter("areaCode");
		List list =null;
		//根据区域名称查找区域代码
		/*String empInfoPlaceOrgan = request.getParameter("empInfoPlaceOrgan");
		String empInfoPlaceOrganCode ="";
		if(empInfoPlaceOrgan!=null && !"".equals(empInfoPlaceOrgan)){
			DataDictionary dd = ddDAO.findByName(empInfoPlaceOrgan);
			empInfoPlaceOrganCode=dd.getDataDictionaryCode();*/
			/*if(empInfoPlaceOrganCode==null || "".equals(empInfoPlaceOrganCode)){
				request.setAttribute("error","您所输入的区域名称不存在，请重新输入！" );
				return mapping.findForward("error");
			}*/
		/*StringBuffer sb = new StringBuffer("");
		for(int i=0;i<uList.size();i++){
			UnitUser uu = (UnitUser)uList.get(i);
			sb.append(uu.getUnitUser().getEmpInfoId());
			sb=sb.append(",");
			
		}
		String eid=sb.toString();
		if(eid.length()>0){
			eid =eid.substring(0,sb.length()-1);
			}
		eid = "'"+eid.replaceAll(",","','")+"'";*/
		
		list = dao.findNotUserPage(areaCode,eForm);
		request.setAttribute("list", list);
		return mapping.findForward("listNotUser");
	}
	
	
	public ActionForward inteStatTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String value = "";
		FunctionMenuDAO dao = new FunctionMenuDAO(request,response);
		List<UserAreaScope> infoList = dao.findByUserId(SystemSession.getSupUserId(request));
		if(infoList != null && infoList.size()>0){
			UserAreaScope info1 =infoList.get(0);
			value = info1.getUserAreaScopeAreaId();
		}
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getPowerAreaTree(request, response, "行政区划", "", "inteStatEmployerInfo.do?method=inteStatList&code=$code",value));		
		return mapping.findForward("inteStatTree");
	}

	
	public ActionForward inteStatList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		String code = request.getParameter("code");
		List list = dao.findAllPage(code, eForm, null);
	//	List list = dao.findInteStatAllPage(request.getParameter("placeOgan"), eForm);
		request.setAttribute("list", list);
		return mapping.findForward("isList");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		EmployerInfo info = dao.findById(request.getParameter("eid"));
		request.setAttribute("eInfo", info);
		return mapping.findForward("detail");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		EmployerInfo info = dao.findById(request.getParameter("eid"));
		request.setAttribute("nowPage",request.getParameter("page"));
		request.setAttribute("eInfo", info);
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		EmployerInfo info = dao.findById(request.getParameter("eid"));
		info.setEmpInfoName(eForm.getEmpInfoName());
		info.setEmpInfoCode(eForm.getEmpInfoCode());
		info.setEmpInfoBusinessRegisOrgan(eForm.getEmpInfoBusinessRegisOrgan());
		info.setEmpInfoShopCard(eForm.getEmpInfoShopCard());
		info.setEmpInfoLandtaxUrl(eForm.getEmpInfoLandtaxUrl());
		info.setEmpInfoLandtaxRegisOrgan(eForm.getEmpInfoLandtaxRegisOrgan());
		info.setEmpInfoShopUrl(eForm.getEmpInfoShopUrl());
		info.setEmpInfoBankOfDeposit(eForm.getEmpInfoBankOfDeposit());
		info.setEmpInfoBankAccount(eForm.getEmpInfoBankAccount());
		info.setEmpInfoUnitType(eForm.getEmpInfoUnitType());
		info.setEmpInfoIndustry(eForm.getEmpInfoIndustry());
//		info.setEmpInfoPlaceOrgan(eForm.getEmpInfoPlaceOrgan());
		info.setEmpInfoPlaceAddress(eForm.getEmpInfoPlaceAddress());
		info.setEmpInfoCorporateRepresent(eForm.getEmpInfoCorporateRepresent());
		info.setEmpInfoRepresentIdNum(eForm.getEmpInfoRepresentIdNum());
		info.setEmpInfoCellPhone(eForm.getEmpInfoCellPhone());
		info.setEmpInfoTelphone(eForm.getEmpInfoTelphone());
		info.setEmpInfoLabManaPrincipal(eForm.getEmpInfoLabManaPrincipal());
		info.setEmpInfoCellPhone1(eForm.getEmpInfoCellPhone1());
		info.setEmpInfoTelphone1(eForm.getEmpInfoTelphone1());
		info.setEmpInfoManHourRegu(eForm.getEmpInfoManHourRegu());
		info.setEmpInfoLowsalaLevel(eForm.getEmpInfoLowsalaLevel());
		info.setEmpInfoAllWorkNum(eForm.getEmpInfoAllWorkNum());
		info.setEmpInfoTownWorkNum(eForm.getEmpInfoTownWorkNum());
		info.setEmpInfoCountryWorkNum(eForm.getEmpInfoCountryWorkNum());
		info.setEmpInfoManNum(eForm.getEmpInfoManNum());
		info.setEmpInfoWomanNum(eForm.getEmpInfoWomanNum());
		info.setEmpInfoContractedNum(eForm.getEmpInfoContractedNum());
		info.setEmpInfoNcontractedNum(eForm.getEmpInfoNcontractedNum());
		info.setEmpInfoGoonContractNum(eForm.getEmpInfoGoonContractNum());
		info.setEmpInfoEmpInjInsNum(eForm.getEmpInfoEmpInjInsNum());
		info.setEmpInfoEndowmentInsNum(eForm.getEmpInfoEndowmentInsNum());
		info.setEmpInfoMedicalInsNum(eForm.getEmpInfoMedicalInsNum());
		info.setEmpInfoUnempInsNum(eForm.getEmpInfoUnempInsNum());
		info.setEmpInfoMaterityInsNum(eForm.getEmpInfoMaterityInsNum());
		info.setEmpInfoNoInsNum(eForm.getEmpInfoNoInsNum());
		info.setEmpInfoIfOrgDealDisputed(eForm.getEmpInfoIfOrgDealDisputed());
		info.setEmpInfoIfContract(eForm.getEmpInfoIfContract());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "EmployerInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getEmpInfoName()));
		logDao.save(s);
		return new ActionForward("/EmployerInfo.do?method=list&code=" + info.getEmpInfoPlaceOrgan()+"&pages="+request.getParameter("pages"), true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		EmployerInfo info = new EmployerInfo();
		info.setEmpInfoName(eForm.getEmpInfoName());
		info.setEmpInfoCode(eForm.getEmpInfoCode());
		info.setEmpInfoBusinessRegisOrgan(eForm.getEmpInfoBusinessRegisOrgan());
		info.setEmpInfoShopCard(eForm.getEmpInfoShopCard());
		info.setEmpInfoLandtaxUrl(eForm.getEmpInfoLandtaxUrl());
		info.setEmpInfoLandtaxRegisOrgan(eForm.getEmpInfoLandtaxRegisOrgan());
		info.setEmpInfoShopUrl(eForm.getEmpInfoShopUrl());
		info.setEmpInfoBankOfDeposit(eForm.getEmpInfoBankOfDeposit());
		info.setEmpInfoBankAccount(eForm.getEmpInfoBankAccount());
		info.setEmpInfoUnitType(eForm.getEmpInfoUnitType());
		info.setEmpInfoIndustry(eForm.getEmpInfoIndustry());
		info.setEmpInfoPlaceOrgan(eForm.getEmpInfoPlaceOrgan());
		info.setEmpInfoPlaceAddress(eForm.getEmpInfoPlaceAddress());
		info.setEmpInfoCorporateRepresent(eForm.getEmpInfoCorporateRepresent());
		info.setEmpInfoRepresentIdNum(eForm.getEmpInfoRepresentIdNum());
		info.setEmpInfoCellPhone(eForm.getEmpInfoCellPhone());
		info.setEmpInfoTelphone(eForm.getEmpInfoTelphone());
		info.setEmpInfoLabManaPrincipal(eForm.getEmpInfoLabManaPrincipal());
		info.setEmpInfoCellPhone1(eForm.getEmpInfoCellPhone1());
		info.setEmpInfoTelphone1(eForm.getEmpInfoTelphone1());
		info.setEmpInfoManHourRegu(eForm.getEmpInfoManHourRegu());
		info.setEmpInfoLowsalaLevel(eForm.getEmpInfoLowsalaLevel());
		info.setEmpInfoAllWorkNum(eForm.getEmpInfoAllWorkNum());
		info.setEmpInfoTownWorkNum(eForm.getEmpInfoTownWorkNum());
		info.setEmpInfoCountryWorkNum(eForm.getEmpInfoCountryWorkNum());
		info.setEmpInfoManNum(eForm.getEmpInfoManNum());
		info.setEmpInfoWomanNum(eForm.getEmpInfoWomanNum());
		info.setEmpInfoContractedNum(eForm.getEmpInfoContractedNum());
		info.setEmpInfoNcontractedNum(eForm.getEmpInfoNcontractedNum());
		info.setEmpInfoGoonContractNum(eForm.getEmpInfoGoonContractNum());
		info.setEmpInfoEmpInjInsNum(eForm.getEmpInfoEmpInjInsNum());
		info.setEmpInfoEndowmentInsNum(eForm.getEmpInfoEndowmentInsNum());
		info.setEmpInfoMedicalInsNum(eForm.getEmpInfoMedicalInsNum());
		info.setEmpInfoUnempInsNum(eForm.getEmpInfoUnempInsNum());
		info.setEmpInfoMaterityInsNum(eForm.getEmpInfoMaterityInsNum());
		info.setEmpInfoNoInsNum(eForm.getEmpInfoNoInsNum());
		info.setEmpInfoIfOrgDealDisputed(eForm.getEmpInfoIfOrgDealDisputed());
		info.setEmpInfoIfContract(eForm.getEmpInfoIfContract());
		info.setEmpInfoDelOper("0");
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "EmployerInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getEmpInfoName()));
		logDao.save(s);
		return new ActionForward("/EmployerInfo.do?method=list&code=" + info.getEmpInfoPlaceOrgan(), true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		EmployerInfo info = dao.findById(request.getParameter("eid"));
		info.setEmpInfoDelOper("1");
		dao.merge(info);
		System.out.println(request.getParameter("page"));
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "EmployerInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getEmpInfoName()));
		logDao.save(s);
		return new ActionForward("/EmployerInfo.do?method=list&code=" + info.getEmpInfoPlaceOrgan()+"&pages="+request.getParameter("page"), true);
	}
	
	public ActionForward find(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		FunctionMenuDAO fdao = new FunctionMenuDAO(request,response);
		String scorp = "";
		List ulist = fdao.findByUserId(SystemSession.getSupUserId(request));
		if(ulist != null && ulist.size() > 0){
			UserAreaScope uas = (UserAreaScope)ulist.get(0);
			scorp = uas.getUserAreaScopeAreaId();
			scorp = scorp.replace(",", "','");
		}
		List list = dao.findAllPage(null, eForm, scorp);
		request.setAttribute("list", list);
		return mapping.findForward("find");
	}
	
	public ActionForward applyList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoForm eForm = (EmployerInfoForm)form;
		FunctionMenuDAO fdao = new FunctionMenuDAO(request,response);
		String scorp = "";
		List ulist = fdao.findByUserId(SystemSession.getSupUserId(request));
		if(ulist != null && ulist.size() > 0){
			UserAreaScope uas = (UserAreaScope)ulist.get(0);
			scorp = uas.getUserAreaScopeAreaId();
			if(scorp!=null && !"".equals(scorp)){
				scorp = scorp.replace(",", "','");
			}
			else{
				scorp="";
			}
		}
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findAllApplyPage(scorp, eForm);
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	public ActionForward applyPass(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		EmployerInfo info = dao.findById(request.getParameter("eid"));
		info.setEmpInfoDelOper("0");
		UnitUserApply apply = dao.findUnitUserApplyByEmpId(info.getEmpInfoId());
		if(apply != null){
			apply.setUUApplyState("1");
			UnitUser user = dao.findUnitUserById(apply.getUUApplyUserId());
			if(user != null){
				user.setUnitUser(info);
				dao.merge(user);
			}
			dao.merge(info);
			dao.merge(apply);
		}else{
			dao.merge(info);
		}
		return new ActionForward("/ApplyEmployerInfo.do?method=applyList", true);
	}
	
	public ActionForward applyUnPass(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		EmployerInfo info = dao.findById(request.getParameter("eid"));
		info.setEmpInfoDelOper("1");
		UnitUserApply apply = dao.findUnitUserApplyByEmpId(info.getEmpInfoId());
		if(apply != null){
			apply.setUUApplyState("2");
			dao.merge(info);
			dao.merge(apply);
		}else{
			dao.merge(info);
		}
		return new ActionForward("/ApplyEmployerInfo.do?method=applyList", true);
	}
	
	public ActionForward tree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String value = "";
		FunctionMenuDAO dao = new FunctionMenuDAO(request,response);
		List<UserAreaScope> infoList = dao.findByUserId(SystemSession.getSupUserId(request));
		if(infoList != null && infoList.size()>0){
			UserAreaScope info1 =infoList.get(0);
			value = info1.getUserAreaScopeAreaId();
		}
		MenuBuilder bu = new MenuBuilder();
	//	request.setAttribute("tree", bu.getCommonAreaTree(request, response, "行政区划", "", "EmployerInfo.do?method=list&code=$code"));
		request.setAttribute("tree", bu.getPowerAreaTree(request, response, "行政区划", "", "EmployerInfo.do?method=list&code=$code",value));		
		return mapping.findForward("tree");
	}
	
	public ActionForward uploadShop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			HashMap map = Upload.upload(request, "employer", ".jpg;.gif");
			if(map.containsKey("fileName")){
				request.setAttribute("fileName", (String)map.get("fileName"));
			}else{
				request.setAttribute("error", "error");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mapping.findForward("upload");
	}
	
	public ActionForward uploadLandtax(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			HashMap map = Upload.upload(request, "employer", ".jpg;.gif");
			if(map.containsKey("fileName")){
				request.setAttribute("fileName", (String)map.get("fileName"));
			}else{
				request.setAttribute("error", "error");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mapping.findForward("upload");
	}
	
	//台帐
	public ActionForward doc1(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findEmployer(request.getParameter("eid"));
		if(list != null && list.size() > 0){
			request.setAttribute("eInfo", list.get(0));
			request.setAttribute("historyList", dao.findEmployerHistoryList(request.getParameter("eid")));
		}
		return mapping.findForward("doc1");
	}
	
	//合同一览表
	public ActionForward doc2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findEmployerContract(request.getParameter("eid"));
		request.setAttribute("list", list);
		return mapping.findForward("doc2");
	}
	
	//续定
	public ActionForward doc3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findEmployer(request.getParameter("eid"));
		if(list != null && list.size() > 0){
			request.setAttribute("eInfo", list.get(0));
			request.setAttribute("cList", dao.findGoOnEmployerContract(request.getParameter("eid")));
		}
		return mapping.findForward("doc3");
	}
	
	//解除
	public ActionForward doc4(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerInfoDAO dao = new EmployerInfoDAO(request, response);
		List list = dao.findEmployer(request.getParameter("eid"));
		if(list != null && list.size() > 0){
			request.setAttribute("eInfo", list.get(0));
			request.setAttribute("cList", dao.findEndEmployerContract(request.getParameter("eid")));
		}
		return mapping.findForward("doc4");
	}
}